import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:people_big_people/utils/log_util.dart';
import 'package:people_big_people/video/mock/video_url_bean.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  final int currentIndex;
  final int index;

  const VideoPage({Key? key, required this.currentIndex, required this.index})
      : super(key: key);

  @override
  State<VideoPage> createState() => _VideoPageState();
}

// Opacity//透明度组件
class _VideoPageState extends State<VideoPage> with WidgetsBindingObserver {
  late VideoPlayerController _videoPlayerController;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    _initVideoControl();
    LogUtil.i("==================>initState");
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _videoPlayerController.play();
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    LogUtil.i("===========>didChangeAppLifecycleState  ${state.name}");
    if (state != AppLifecycleState.resumed) {
      _videoPlayerController.pause();
    } else {
      _videoPlayerController.play();
    }
    setState(() {});
  }

  @override
  void dispose() {
    LogUtil.i("==================>dispose   current  ${widget.currentIndex}");
    WidgetsBinding.instance.removeObserver(this);
    _videoPlayerController.dispose();
    super.dispose();
  }

  ///初始化控制
  _initVideoControl() {
    if (mounted) {
      _videoPlayerController =
          VideoPlayerController.network(VideoUrlBean.fetchVideo()[0].url)
            ..addListener(() {
              setState(() {});
            })
            ..setLooping(true)
            ..initialize().then((_) {
              setState(() {});
            });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [videoWidget(), clickWidget(), videoSeek()],
      ),
    );
  }

  ///视频播放组件
  Widget videoWidget({String? videoUrl}) => Positioned(
          child: Center(
        child: _videoPlayerController.value.isInitialized
            ? AspectRatio(
                aspectRatio: _videoPlayerController.value.aspectRatio, //屏幕宽高比
                child: VideoPlayer(_videoPlayerController),
              )
            : AspectRatio(
                aspectRatio: 9 / 16,
                // MediaQuery.of(context).size.width / MediaQuery.of(context).size.height, //屏幕宽高比
                child: ExtendedImage.network(
                  "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201704%2F01%2F20170401213613_2WLkS.thumb.700_0.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664433970&t=3cfa16046f557324b28eca00d527630f",
                  cache: true,
                ),
              ),
      ));

  ///进度条组件
  Widget videoSeek() => Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        child: VideoProgressIndicator(_videoPlayerController,
            allowScrubbing: true),
      );

  ///遮盖图片
  Widget frontImg = Positioned(child: Container());

  ///暂停按钮
  Widget pauseImg = Positioned(child: Container());

  ///触摸组件
  Widget clickWidget() => GestureDetector(
        onDoubleTap: () {
          //双击收藏
          LogUtil.i("双击收藏");
        },
        onTap: () {
          //单击显示暂停
          if (_videoPlayerController.value.isPlaying) {
            _videoPlayerController.pause();
            LogUtil.i("单击暂停---------------------->");
          } else {
            _videoPlayerController.play();
            LogUtil.i("单击播放---------------------->");
          }
        },
      );
}
